package cn.blossoming.controller.web;

import cn.blossoming.controller.vo.ApiResult;
import cn.blossoming.controller.vo.tips.TipsCommentList;
import cn.blossoming.controller.vo.tips.TipsListVo;
import cn.blossoming.controller.vo.tips.TipsVo;
import cn.blossoming.service.dto.Tips;
import cn.blossoming.service.dto.TipsComment;
import cn.blossoming.service.tips.TipsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/tips", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TipsController extends BaseController {
    @Autowired
    private TipsService tipsService;

    /**
     *  获取最新贴士列表
     */
    @GetMapping(path = "/")
    public ApiResult<TipsListVo> getTipsList(@RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                             @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<Tips> tipsList = tipsService.findPage(new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        TipsListVo vo = new TipsListVo();
        vo.setTipsList(tipsList);
        return ApiResult.builder(vo);
    }

    /**
     * 获取贴士详情
     */
    @GetMapping(path = "/{id}")
    public ApiResult<TipsVo> getTipsById(@PathVariable int id) {
        Tips tips = tipsService.getTipsById(id);
        TipsVo tipsVo = new TipsVo();
        tipsVo.setTips(tips);
        return ApiResult.builder(tipsVo);
    }

    /**
     * 贴士点赞
     */
    @PostMapping(path = "/{id}/praise")
    public ApiResult<TipsVo> tipsPraise(@PathVariable int id) {
        Tips tips = tipsService.praiseTips(id);
        TipsVo tipsVo = new TipsVo();
        tipsVo.setTips(tips);
        return ApiResult.builder(tipsVo);
    }

    @GetMapping(path = "/{id}/comment")
    public ApiResult<TipsCommentList> tipsComment(@PathVariable int id,
                                                    @RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                                    @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<TipsComment> comments = tipsService.getCommentsByTipsId(id, new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        TipsCommentList commentList = new TipsCommentList();
        commentList.setTipsCommentList(comments);
        return ApiResult.builder(commentList);
    }

    @GetMapping(path = "/comment/{id}")
    public ApiResult<TipsCommentList> tipsCommentComment(@PathVariable int id,
                                                           @RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                                           @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<TipsComment> comments = tipsService.getTipsCommentCommentByCommentId(id, new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        TipsCommentList commentList = new TipsCommentList();
        commentList.setTipsCommentList(comments);
        return ApiResult.builder(commentList);
    }

    @PostMapping(path = "/praise/comment/{id}")
    public ApiResult<?> tipsCommentPraise(@PathVariable int id) {
        tipsService.tipsCommentPraise(id);
        return ApiResult.builder(null);
    }

    @PostMapping(path = "/{id}/comment")
    public ApiResult<?> addTipsComment(@PathVariable int id, @RequestBody TipsComment comment) {
        tipsService.addTipsComment(id, comment.getContent());
        return ApiResult.builder(null);
    }

    @PostMapping(path = "/comment/{id}")
    public ApiResult<?> addTipsCommentComment(@PathVariable int id, @RequestBody TipsComment comment) {
        tipsService.addTipsCommentComment(id, comment.getContent(), comment.getTipsId());
        return ApiResult.builder(null);
    }
}
